/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dairy.database.DatabaseInterface;
import com.dairy.helpers.DatabaseHelper;
import com.dairy.structures.Orders;
import javax.servlet.http.HttpSession;

/**
 *
 * @author tecton
 */
public class ModifyOrdersAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserOrdersForm userorder = (UserOrdersForm)form;
        DatabaseInterface db = DatabaseHelper.getUserDatabase(servlet);
        HttpSession session = request.getSession();
        String username = session.getAttribute("username").toString();
        userorder.setOrders(db.getOrders(username));
        String product = request.getParameter("name");
        String nameCompany = request.getParameter("name_company");
        String nameProduct = request.getParameter("name_product");
        if (product != null && !product.equals(""))
        {
            userorder.setOrder(db.getOrder(username, product));
            request.setAttribute("show", "true");
            return mapping.findForward(SUCCESS);
        }
        else if (nameCompany != null && nameProduct != null
                && !nameCompany.equals("") && !nameProduct.equals(""))
        {
            Orders order = new Orders();
            order.id = db.getOrderMaxID() + 1;
            order.duration = 0;
            order.day_of_week = 1;
            order.name = nameProduct;
            order.username = username;
            order.price = db.getPrice(nameCompany, nameProduct);
            userorder.setOrder(order);
            request.setAttribute("show", "true");
            return mapping.findForward(SUCCESS);
        }
        if (userorder.getOrder() == null || userorder.getOrder().username.equals("")
                || request.getParameter("number") == null)
            request.setAttribute("show", "false");
        else
        {
            Orders order = userorder.getOrder();
            order.number = Integer.parseInt(request.getParameter("number"));
            order.day_of_week = Integer.parseInt(request.getParameter("dow"));
            order.duration = Short.parseShort(request.getParameter("duration"));
            order.id_company = Integer.parseInt(request.getParameter("ic"));
            request.setAttribute("show", "true");
            if (order.duration != 0 && order.day_of_week == 0
                    || order.duration == 0 && order.day_of_week > 0 && order.day_of_week < 8)
            {
                db.updateOrder(userorder.getOrder());
                request.setAttribute("success", true);
            }
            else
                request.setAttribute("success", false);
        }
        return mapping.findForward(SUCCESS);
    }
}
